package com.show.antenna.service.impl;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.show.antenna.bean.*;
import com.show.antenna.mapper.AntenaMapper;
import com.show.antenna.mapper.ApprovalMapper;
import com.show.antenna.mapper.RasterMapper;
import com.show.antenna.mapper.UserMapper;
import com.show.antenna.service.AntenaService;

import com.show.antenna.tool.passMD5;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.springframework.stereotype.Service;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (Antena)表服务实现类
 */
@Service
public class AntenaServiceImpl implements AntenaService {
    @Resource
    AntenaMapper antenaMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    RasterMapper rasterMapper;

    @Resource
    ApprovalMapper approvalMapper;

    /**
     * 用户登录
     */
    @Override
    public List<UserInfoEntity> userLogin(UserInfoEntity userInfoEntity) {
        List<UserInfoEntity> returnUserList;
        try {
            String password = userInfoEntity.getPassword();
            String md5Password = passMD5.md52(password);
            userInfoEntity.setPassword(md5Password);
            returnUserList = userMapper.userLogin(userInfoEntity);
        } catch (Exception ee) {
            System.out.println(ee);
            return null;
        }
        return returnUserList;
    }

    /**
     * 用户登录
     */
    @Override
    public String searchUser(UserInfoEntity userInfoEntity) {
        String success = "";
        try {
            List<UserInfoEntity> user = userMapper.searchUser(userInfoEntity);
            if (user.size() > 0) {
                success = "notNull";
            } else {
                success = "failed";
            }
        } catch (Exception ee) {
            success = "failed";
            System.out.println(ee);
            return null;
        }
        return success;
    }

    /**
     * 注册用户
     */
    @Override
    public String userRegister(UserInfoEntity userInfoEntity) {
        String success = "";
        try {
            List<UserInfoEntity> user = userMapper.searchUser(userInfoEntity);
            if (user.size() > 0) {
                success = "notNull";
            } else {
                String password = userInfoEntity.getPassword();
                String md5Password = passMD5.md52(password);
                userInfoEntity.setPassword(md5Password);
                //创建唯一ID
                UUID uuid = UUID.randomUUID();
                userInfoEntity.setUserid(uuid.toString());
                userMapper.userRegister(userInfoEntity);
                success = "success";
            }
        } catch (Exception e) {
            success = "failed";
            System.out.println(e);
        }
        return success;
    }

    /**
     * 获取用户信息
     */
    @Override
    public List<UserInfoEntity> getUserInfo() {
        return userMapper.getUserInfo();
    }

    /**
     * 更新用户类型
     */
    @Override
    public String updateUserInfo(UserInfoEntity userInfoEntity) {
        String success = "ok";
        try {
            userMapper.updateUserInfo(userInfoEntity);
        } catch (Exception e) {
            success = "error";
            System.out.println(e);
        }
        return success;
    }

    /**
     * 新增申请
     */
    @Override
    public String addApprovalInfo(ApprovalEntity approvalEntity) {
        String success = "ok";
        try {
            List<ApprovalEntity> approvalEntities = approvalMapper.getApprovalInfoByInfo(approvalEntity);
            if (approvalEntities.size() > 0) {
                approvalEntity.setRstatus("待审批");
                approvalMapper.updataApprovalInfo(approvalEntity);
            } else {
                approvalMapper.addApprovalInfo(approvalEntity);
            }
        } catch (Exception e) {
            success = "error";
            System.out.println(e);
        }
        return success;
    }

    /**
     * 查询影像数据
     *
     * @return 对象列表
     */
    @Override
    public List<RasterEntity> getRaster() {
        return rasterMapper.getRaster();
    }

    /**
     * 查询影像数据
     *
     * @return 对象列表
     */
    @Override
    public List<RasterEntity> getRaster2(ApprovalEntity approvalEntity) {
        String applicant = approvalEntity.getRapplicat();
        List<RasterEntity> noStatusRaster = rasterMapper.getRaster();
        for (int i = 0; i < noStatusRaster.size(); i++) {
            RasterEntity data = noStatusRaster.get(i);
            int rid = data.getRid();
            ApprovalEntity searchApprovalEntity = new ApprovalEntity();
            searchApprovalEntity.setRapplicat(applicant);
            searchApprovalEntity.setRid(rid);
            List<ApprovalEntity> approvalEntities = approvalMapper.getApprovalInfoByUser(searchApprovalEntity);
            if (approvalEntities.size() > 0) {
                for (int r = 0; r < approvalEntities.size(); r++) {
                    ApprovalEntity appdata = approvalEntities.get(r);
                    String Status = appdata.getRstatus();
                    data.setRstatus(Status);
                }
            } else {
                data.setRstatus("");
            }
        }
        return noStatusRaster;
    }


    /**
     * 获取带审批的影像数据 过滤所属属性、所属区域
     */
    public List<RasterEntity> getRasterByType(RasterTypeEntity rasterTypeEntity) {

        String applicant = rasterTypeEntity.getRapplicat();
        String rarea = rasterTypeEntity.getRarea();
        String rtype = rasterTypeEntity.getRtype();
        List<RasterEntity> noStatusRaster = null;
        if (rarea != null && rtype == null) {
            noStatusRaster = rasterMapper.getRasterByArea(rasterTypeEntity);
        } else if (rtype != null && rarea == null) {
            noStatusRaster = rasterMapper.getRasterByType(rasterTypeEntity);
        } else if (rarea != null && rtype != null) {
            noStatusRaster = rasterMapper.getRasterByTypeAndArea(rasterTypeEntity);
        }

        for (int i = 0; i < noStatusRaster.size(); i++) {
            RasterEntity data = noStatusRaster.get(i);
            int rid = data.getRid();
            ApprovalEntity searchApprovalEntity = new ApprovalEntity();
            searchApprovalEntity.setRapplicat(applicant);
            searchApprovalEntity.setRid(rid);
            List<ApprovalEntity> approvalEntities = approvalMapper.getApprovalInfoByUser(searchApprovalEntity);
            if (approvalEntities.size() > 0) {
                for (int r = 0; r < approvalEntities.size(); r++) {
                    ApprovalEntity appdata = approvalEntities.get(r);
                    String Status = appdata.getRstatus();
                    data.setRstatus(Status);
                }
            } else {
                data.setRstatus("");
            }
        }
        return noStatusRaster;
    }

    /**
     * 获取带审批的影像数据 过滤所属属性和所属区域
     */
    public List<RasterEntity> getRasterByTypeAndArea(RasterTypeEntity rasterTypeEntity) {
        String applicant = rasterTypeEntity.getRapplicat();
        List<RasterEntity> noStatusRaster = rasterMapper.getRasterByTypeAndArea(rasterTypeEntity);
        for (int i = 0; i < noStatusRaster.size(); i++) {
            RasterEntity data = noStatusRaster.get(i);
            int rid = data.getRid();
            ApprovalEntity searchApprovalEntity = new ApprovalEntity();
            searchApprovalEntity.setRapplicat(applicant);
            searchApprovalEntity.setRid(rid);
            List<ApprovalEntity> approvalEntities = approvalMapper.getApprovalInfoByUser(searchApprovalEntity);
            if (approvalEntities.size() > 0) {
                for (int r = 0; r < approvalEntities.size(); r++) {
                    ApprovalEntity appdata = approvalEntities.get(r);
                    String Status = appdata.getRstatus();
                    data.setRstatus(Status);
                }
            } else {
                data.setRstatus("");
            }
        }
        return noStatusRaster;
    }


    /**
     * 获取带审批的影像数据 过滤时间段
     */
    @Override
    public List<RasterEntity> getRasterByTime(RasterTypeEntity rasterTypeEntity) {
        String applicant = rasterTypeEntity.getRapplicat();
        List<RasterEntity> rasterEntityList = rasterMapper.getRasterByTime(rasterTypeEntity);
        for (int i = 0; i < rasterEntityList.size(); i++) {
            RasterEntity data = rasterEntityList.get(i);
            int rid = data.getRid();
            ApprovalEntity searchApprovalEntity = new ApprovalEntity();
            searchApprovalEntity.setRapplicat(applicant);
            searchApprovalEntity.setRid(rid);
            List<ApprovalEntity> approvalEntities = approvalMapper.getApprovalInfoByUser(searchApprovalEntity);
            if (approvalEntities.size() > 0) {
                for (int r = 0; r < approvalEntities.size(); r++) {
                    ApprovalEntity appdata = approvalEntities.get(r);
                    String Status = appdata.getRstatus();
                    data.setRstatus(Status);
                }
            } else {
                data.setRstatus("");
            }
        }
        return rasterEntityList;
    }

    /**
     * 获取带审批的影像数据 过滤时间段、所属属性和所属区域
     */
    @Override
    public List<RasterEntity> getRasterByTypeAreaTime(RasterTypeEntity rasterTypeEntity) {
        String applicant = rasterTypeEntity.getRapplicat();
        List<RasterEntity> rasterEntityList = rasterMapper.getRasterByTypeAreaTime(rasterTypeEntity);
        for (int i = 0; i < rasterEntityList.size(); i++) {
            RasterEntity data = rasterEntityList.get(i);
            int rid = data.getRid();
            ApprovalEntity searchApprovalEntity = new ApprovalEntity();
            searchApprovalEntity.setRapplicat(applicant);
            searchApprovalEntity.setRid(rid);
            List<ApprovalEntity> approvalEntities = approvalMapper.getApprovalInfoByUser(searchApprovalEntity);
            if (approvalEntities.size() > 0) {
                for (int r = 0; r < approvalEntities.size(); r++) {
                    ApprovalEntity appdata = approvalEntities.get(r);
                    String Status = appdata.getRstatus();
                    data.setRstatus(Status);
                }
            } else {
                data.setRstatus("");
            }
        }
        return rasterEntityList;
    }

    /**
     * 新增影像数据
     */
    @Override
    public String addRaster(RasterEntity rasterEntity) {
        String success = "error";
        try {
            rasterMapper.addRaster(rasterEntity);
            List<RasterEntity> rasterEntityList = rasterMapper.getRasterBySomeInfo(rasterEntity);
            for (int i = 0; i < rasterEntityList.size(); i++) {
                RasterEntity data = rasterEntityList.get(i);
                success = data.getRid() + "";
            }
        } catch (Exception e) {
            success = "error";
            System.out.println(e);
        }
        return success;
    }


    /**
     * 更新审批状态
     */
    @Override
    public String updataApprovalInfo(ApprovalEntity approvalEntity) {
        String success = "ok";
        try {

            approvalMapper.updataApprovalInfo(approvalEntity);
        } catch (Exception e) {
            success = "error";
            System.out.println(e);
        }
        return success;
    }

    /**
     * 获取影像数据
     */
    @Override
    public List<ApprovalEntity> getApprovalInfo() {
        return approvalMapper.getApprovalInfo();
    }

    /**
     * 根据ID插入影像下载地址
     */
    @Override
    public String updateRasterPath(RasterEntity rasterEntity) {
        String success = "ok";
        try {
            rasterMapper.updateRasterPath(rasterEntity);
        } catch (Exception e) {
            success = "error";
            System.out.println(e);
        }
        return success;
    }

    /**
     * 根据ID插入例图地址
     */
    @Override
    public String updatePicPath(RasterEntity rasterEntity) {
        String success = "ok";
        try {
            rasterMapper.updatePicPath(rasterEntity);
        } catch (Exception e) {
            success = "error";
            System.out.println(e);
        }
        return success;
    }
}